package com.teamwings.modules.oss.controller;


import com.teamwings.common.vo.Result;
import com.teamwings.modules.oss.entity.OssFile;
import com.teamwings.modules.oss.service.IOssService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author admin
 * @since 2021-05-06
 */
@RestController
@Api(tags = "对象存储服务")
@RequestMapping("/form/oss")
@Slf4j
public class OssController {

    @Autowired
    private IOssService ossService;

    @ApiOperation(value = "文件上传到minio服务", notes = "文件上传到minio服务")
    @PostMapping(value = "upload")
    public Result<?> upload(
            @RequestParam(value = "file") MultipartFile file,
            @RequestParam(value = "bizPath", required = false) String bizPath
    ) {
        Result<?> result = new Result<>();
        OssFile ossFile = ossService.uploadFile(file, bizPath);
        result.setMessage(ossFile.getUrl());
        result.setSuccess(true);
        return result;
    }

    @ApiOperation(value = "文件列表下载", notes = "文件列表下载")
    @PostMapping(value = "downloadFiles")
    public void downloadFiles(
            HttpServletResponse response,
            @RequestParam(value = "bizPath") List<String> bizPath
    ) {
        ossService.downloadFiles(bizPath, response);
    }

    @ApiOperation(value = "文件下载", notes = "文件下载")
    @PostMapping(value = "downloadFile")
    public void downloadFile(
            HttpServletResponse response,
            @RequestParam(value = "bizPath") String bizPath
    ) {
        ossService.downloadFile(bizPath, response);
    }


}
